import requests
import re
import os

class BaiDuSpider():
    def __init__(self):
        self.url = "https://tieba.baidu.com/f?kw={}&pn={}"
        self.headers = {
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36"
        }
        if not os.path.exists("baidu"):
            os.mkdir("baidu")

    def send_request(self, url):
        print("正在发送{}".format(url))
        response = requests.get(url, headers=self.headers)
        return response

    def parse_content(self, response):
        content = response.text
        # with open("bd.html", "w", encoding="utf8") as f:
        #     f.write(content)
        detail_url = re.findall('href="(/p/\d+)"', content, re.S)
        for url in detail_url:
            full_detail_url = "https://tieba.baidu.com" + url
            # 发起详情页请求
            response_detail = self.send_request(full_detail_url)
            if response_detail.status_code == 200:
                # 解析详情页
                self.parse_detail_content(response_detail)

    def parse_detail_content(self, response_detail):
        # with open("bb_detail.html", "w", encoding="utf8") as f:
        #     f.write(response_detail.text)
        img_urls = re.findall('<img.*?class="BDE_Image".*?src="(.*?)"', response_detail.text, re.S)
        for img in img_urls:
            # 发起图片地址请求
            response = self.send_request(img)
            if response.status_code == 200:
                self.save_content(response,img)

    def save_content(self,response,url):
        file_name = re.search("(.{10}.jpg)", url).group(0)
        print("正在保存{}".format(file_name))
        with open("baidu/"+file_name,"wb") as f:
            # 必须用二进制
            f.write(response.content)

    def start(self):
        name = input("请输入要爬取的贴吧的名字")
        for i in range(1,10):
            full_url = self.url.format(name,(i-1)*50)
            response = self.send_request(full_url)
            if response.status_code == 200:
                self.parse_content(response)


if __name__ == '__main__':
    bds = BaiDuSpider()
    bds.start()


"""
    0    1
    50    2
    100   3
    
    (page-1)*50

"""